package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.cl.pojo.Notice;
import com.cl.pojo.Vaccinator;
import com.cl.service.impl.NoticeServiceImpl;
import com.cl.service.impl.ScheduledService;
import com.cl.service.impl.VaccinatorServiceImpl;
import com.cl.utils.MsgUtil;
import com.cl.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Month;
import java.time.Period;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NoticeController {
    @Autowired
    private NoticeServiceImpl noticeService;
    @Autowired
    private VaccinatorServiceImpl vaccinatorService;
    @Autowired
    private TimeUtil timeUtil;
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private ScheduledService scheduledService;
    @PostMapping("/insertNotice")
    public String insertNotice(@RequestParam("form")String form){
        JSONObject jsonObject = JSON.parseObject(form);
        String todayTime = timeUtil.getTodayTime();
        Notice notice = new Notice();
        notice.setnTitle((String) jsonObject.get("title"));
        notice.setnContent((String) jsonObject.get("content"));
        notice.setnTime(todayTime);

        int insert = noticeService.insert(notice);
        if (insert>0){
            return JSON.toJSONString(msgUtil.success("成功"));
        }
        return JSON.toJSONString(msgUtil.fail("失败"));
    }
    @GetMapping("/getAllNotice")
    public String getAllNotice(@RequestParam(value = "time",required = false)String time,@RequestParam("page") int page,@RequestParam("limit") int limit){
        List<Notice> notices;
        int size = noticeService.countAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        if (time==null){
            notices = noticeService.queryAllNotice(null, (page - 1) * limit, limit);
            map.put("data",notices);
            return JSON.toJSONString(map);
        }else {
            notices = noticeService.queryAllNotice(time, (page - 1) * limit, limit);
            if (notices.size()==0){
                List<Notice> notices1 = noticeService.queryAllNotice(null, (page - 1) * limit, limit);
                map.put("data",notices1);
                return JSON.toJSONString(map);
            }else {
                map.put("data",notices);
                return JSON.toJSONString(map);
            }
        }
    }
    @PostMapping("/modifyNotice")
    public String modifyNotice(@RequestParam("title")String title,@RequestParam("content")String content,@RequestParam("time")String time,@RequestParam("nNo")String no){
        Notice notice = new Notice();
        notice.setnTime(time);
        notice.setnTitle(title);
        notice.setnContent(content);
        notice.setnNo(Integer.parseInt(no));
        int i = noticeService.modifyNotice(notice);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("成功"));
        }
        return JSON.toJSONString(msgUtil.fail("失败"));
    }
    @PostMapping("/deleteNotice")
    public String deleteNotice(@RequestParam("nNo")String nNo){
        int i = noticeService.deleteNotice(Integer.parseInt(nNo));
        if (i>0){
            return JSON.toJSONString(msgUtil.success("成功"));
        }
        return JSON.toJSONString(msgUtil.fail("失败"));
    }
    @PostMapping("/getNotic")
    public String getNotic(@RequestParam("curr")int curr,@RequestParam("limit")int limit){
        List<Notice> notices = noticeService.queryAllNotice(null, (curr - 1) * limit, limit);
        if (notices.size()!=0){
            return JSON.toJSONString(notices);
        }
        return JSON.toJSONString(msgUtil.fail("失败"));
    }
    @PostMapping("/getCount")
    public String getCount(){
        int i = noticeService.countAll();
        return JSON.toJSONString(i);
    }
    @PostMapping("/sendAdminNotice")
    public void sendAdminNotice(@RequestParam("title")String title,@RequestParam("content")String content,@RequestParam("age")String age){
        String s = age.substring(age.length() - 1);
        int ages = Integer.parseInt(age.substring(0, age.length() - 1));
        List<Vaccinator> vaccinators = vaccinatorService.queryAllTime();
        for (Vaccinator vaccinator : vaccinators) {
            String time = vaccinator.getsAppointmenttime();
            String age1 = vaccinator.getsAge();
            String[] split = time.split("-");
            Period period = timeUtil.calculateTimeDifferenceByPeriod(Integer.parseInt(split[0]), Month.of(Integer.parseInt(split[1])), Integer.parseInt(split[2]));
            if (s.equals("月")){
                String i = String.valueOf(ages - period.getMonths());
                String s1 = i.concat("月");
                List<String> emails = vaccinatorService.queryByAge(s1);
                scheduledService.sendToAge(title,content,emails);
            }else {
                String i = String.valueOf(ages - period.getYears());
                String s1 = i.concat("年");
                List<String> strings = vaccinatorService.queryByAge(s1);
                scheduledService.sendToAge(title,content,strings);
            }
        }
    }
}
